<template>
    <div>
        <yh-dialog
            drag
            :class="{ 'dialog-content': !isFullScreen }"
            :visible="riskResultShow1"
            @close="closedDialog"
            :close-on-click-modal="false"
            :fullscreen="isFullScreen"
        >
            <div slot="title" class="dialog-title header">
                <span>风控下单结果信息:</span>
                <i class="el-icon-full-screen" style="display: inline-block; cursor: pointer" @click="handleFullScreen"></i>
            </div>

            <div style="display: flex; justify-content: space-between; padding: 10px">
                <span>{{ '风控下单结果信息' }}</span>
                <el-checkbox v-model="isComplianceShow" @change="handleComplianceShow">显示合规</el-checkbox>
            </div>

            <RiskResult
                :riskResultTable="riskResultTableTemp"
                @handleTableReturn="handleTableReturn"
                :key="riskResultTableKey + 'riskResultTableKey'"
                v-if="riskResultShow1"
            ></RiskResult>
            <SingleDetail :transRow="transRow" style="margin: 10px 0" :key="branchResultsNumKey + 'branchResultsNumKey'"></SingleDetail>
            <div slot="footer">
                <div style="text-align: center">
                    <div class="result-span">
                        本次风控下单结果为: <StatusTag :code="riskResultThresholdType" style="font-size: 18px"></StatusTag>
                        &nbsp;,请选择以下操作：
                        <div style="display: flex; justify-content: center; margin: 5px 0">
                            <el-button v-if="['1', '2'].includes(riskResultThresholdType)" type="success" @click="continuedOrder" size="mini"
                                >继续下达</el-button
                            >
                            <el-button type="danger" @click="endOrder" size="mini">关闭</el-button>
                        </div>
                    </div>
                </div>
            </div>
        </yh-dialog>
    </div>
</template>
<script lang="ts">
import Invest from '@/common/dict/invest';
import StatusTag from '@/pages/risk-measure/components/status-tag.vue';
import { SGU_CACHE } from '@/utils/diction-const-util';
import { Component, Vue, Prop, Watch } from 'vue-property-decorator';
import SingleDetail from '../invest-manage/single-detail.vue';
import { getInstructionResultBranchDetail } from '@/common/api/instruction';
import RiskResult from './risk-result.vue';
@Component({
    name: 'RiskTrialResult',
    components: {
        RiskResult,
        StatusTag,
        SingleDetail,
    },
})
export default class RiskOrderResultAfter extends Vue {
    @Prop() res;
    @Prop() riskResultThresholdType;

    private riskResultShow1: boolean = false;

    private riskResultTable: any[] = []; // 风险试算结果

    private riskResultTableTemp: any[] = []; // 风险试算显示结果

    stockCodeSelectData: any[] = []; // 证券代码下拉框数据

    fundCodeSelectData: any[] = []; // 产品序号下拉框数据

    entryType = {}; // 风险类型字典

    compareDirection = {}; // 比较方向字典

    private isComplianceShow: boolean = false; // 是否显现合规

    private riskResultTableKey: number = 0; // 表格渲染Key

    Invest: any = new Invest();

    private transRow: any[] = []; // 传值详情

    private isFullScreen: boolean = false; // 是否全屏

    private branchResultsNumKey: number = 0;

    private ThresholdType = {
        '0': '合规',
        '1': '警告',
        '2': '送审',
        '3': '禁止',
        '4': '错误',
    };

    async mounted() {
        // 获取产品信息 (产品序号下拉框数据)
        this.fundCodeSelectData = await SGU_CACHE('FundInfo', '/fundInfo/list', []);
        // 获取风险类型字典
        this.entryType = this.Invest.entryType;
        // 获取比较方向字典
        this.compareDirection = this.Invest.CompareDirection;
    }

    @Watch('res', { immediate: true, deep: true })
    onWatchRes() {
        console.log('来了======', this.res);
        this.riskResultShow1 = true;

        let data = JSON.parse(JSON.stringify(this.res)) || [];

        data = data.sort((a, b) => parseInt(b.thresholdType) - parseInt(a.thresholdType));

        this.riskResultTable = data;

        if (this.riskResultTable.length > 0) {
            let value = this.riskResultTable.find(item => item.thresholdType !== '0' && item.thresholdType != '4');
            this.handleTableReturn(value);
        }
        this.handleComplianceShow();
    }
    // 显示合规处理
    private handleComplianceShow() {
        let data = [];

        // if (!this.isComplianceShow) {
        //     data = this.riskResultTable.filter(item => item.thresholdType !== '0' && item.thresholdType != '4');
        // } else {
        //     data = this.riskResultTable.filter(item => item.thresholdType != '4');
        // }
        if (!this.isComplianceShow) {
            data = this.riskResultTable.filter(item => item.thresholdType !== '0');
        } else {
            data = this.riskResultTable;
        }
        this.$set(this, 'riskResultTableTemp', data);
        this.riskResultTableKey++;
    }

    // 继续下达
    private continuedOrder(): void {
        this.riskResultShow1 = false;
        this.$emit('riskResultContinue');
    }

    // 终止下达
    private endOrder(): void {
        this.riskResultShow1 = false;
        this.$emit('riskResultCancel');
    }

    // 关闭弹窗,终止下达
    private closedDialog(): void {
        // 加个这个判断防止重复请求
        if (this.riskResultShow1) {
            this.endOrder();
        }
    }

    // 点击行返回操作
    private handleTableReturn(row): void {
        console.log('点击行', row);
        getInstructionResultBranchDetail(row.requestId, row.entryKey, row.groupCode, row.stockCode, true).then(res => {
            let branchDetailData = JSON.parse(JSON.stringify(row));
            branchDetailData.branchCalculateResults = res || [];
            branchDetailData.branchCalculateResults.forEach(item => {
                item.branchCalulateTypeResults = JSON.parse(item.branchCalulateTypeResults || '{}');
                item.calculateDetails = JSON.parse(item.calculateDetails || '{}');
            });
            console.log('branchDetailData', branchDetailData);
            this.branchResultsNumKey++;
            this.transRow = branchDetailData;
        });
    }

    // 是否全屏
    private handleFullScreen() {
        this.isFullScreen = !this.isFullScreen;
    }
}
</script>
<style lang="scss" scoped>
.result-span {
    font-size: 20px;
    display: block;
    & /deep/ .el-tag.el-tag--mini {
        font-size: 18px;
    }
}

#bid-detail-dialog {
    /deep/ .el-dialog {
        min-width: 1000px;
        .el-dialog__body {
            max-height: none;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 42px;
            line-height: 42px;
            margin-right: 20px;
        }
    }
}

/deep/ .el-dialog {
    min-width: 1000px;
    .el-dialog__body {
        padding: 0 10px;
        max-height: 100vh;
    }
    .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 42px;
        line-height: 42px;
        margin-right: 20px;
    }
}
.dialog-content /deep/ .el-dialog {
    margin-top: 1vh !important;
}
/deep/ .vxe-table .vxe-table--main-wrapper .vxe-table--body-wrapper {
    height: 26vh !important;
}
</style>
